package com.cn.zpark;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * 获取请求头信息
 */
@WebServlet("/req2.do")
public class RequestDemo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应数据类型
        response.setContentType("text/html;charset=utf-8");
        // 获取写出流
        PrintWriter writer = response.getWriter();
        writer.println("<table border='1' cellspacing='0' cellpadding='10'>");
        writer.println("<tr><th>请求头名字</th><th>请求头值</th></tr>");

        // 获取请求头数据
        Enumeration<String> headerNames = request.getHeaderNames();

        while (headerNames.hasMoreElements()) {
            // 取出请求头名字
            String headName = headerNames.nextElement();
            // 根据请求头名字获取对应的值
            String value = request.getHeader(headName);
            writer.println("<tr><td>"+ headName +"</td><td>"+ value +"</td></tr>");
        }

        writer.println("</table>");
        // 关闭资源
        writer.close();


    }
}
